const MYSQL = require("../../sql/mysql");
// MYSQL数据库配置信息
const MYSQLConfig = require("../../sql/database");
let sqlName = MYSQLConfig.datasheetName.roles;
/**
 * 获取角色信息
 * @param {object} params
 * @param {string} [params.id] 角色id
 * @param {string} [params.name] 角色名称
 * @param {string} [params.scope] 角色标识
 * @return {Promise<{code:string,msg:string}>}
 */
module.exports = async (params = {}) => {
  try {
    let { id, name } = params;
    let query = `SELECT * FROM ${sqlName} WHERE `;
    if (id) {
      query += `id = ${id}`;
    } else if (name) {
      query += `name = '${name}'`;
    } else if (scope) {
      query += `scope = '${name}'`;
    } else {
      throw {
        code: 500,
        msg: "暂不支持该方式获取用户信息",
      };
    }
    try {
      let result = await MYSQL.promise().query(query);
      let list = result[0];
      if (list.length) {
        let info = list[0];
        return {
          code: 0,
          msg: "获取成功",
          data: info,
          exist: true,
        };
      } else {
        return {
          code: 0,
          msg: "获取成功",
          data: [],
          exist: false,
        };
      }
    } catch (err) {
      throw {
        code: 200,
        msg: "查询失败，数据库操作不成功",
      };
    }
  } catch (error) {
    throw error;
  }
};
